﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;

namespace Okra.NstdaGisTool {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        public MainWindow() {
            // Show splash screen
            //{
            //    SplashScreen sp = new SplashScreen(@"Images/NstdaBanner.png");
            //    sp.Show(false, true);
            //    System.Threading.Thread.Sleep(5);
            //    sp.Close(TimeSpan.FromSeconds(2));
            //}
            //{
            //    SplashScreen sp = new SplashScreen(@"Images/Iggy.jpg");
            //    sp.Show(false, true);
            //    sp.Close(TimeSpan.FromSeconds(1));
            //}


            InitializeComponent();
            _listBox.Items.Clear();

            // Show this computer fingerprint
            //_txtInfo.Text = "Your computer fingerprint:\n" + FingerPrint.Value();

            // Setup Gdal
            GdalBinding.SetupGdalBindings();

            // Load persistent window size and config
            this.Width = Properties.Settings.Default.SavedWindowWidth;
            this.Height = Properties.Settings.Default.SavedWindowHeight;
            _rowDef0.Height = new GridLength(Properties.Settings.Default.SavedGridSplitterPos, GridUnitType.Pixel);
        }

        private void _listBox_Drop(object sender, DragEventArgs e) {
            if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
                string[] strDroppedFilePaths = e.Data.GetData(DataFormats.FileDrop) as string[];
                foreach (string strPath in strDroppedFilePaths) {
                    if (!_listBox.Items.Contains(strPath)) {
                        _listBox.Items.Add(strPath);
                    }
                }
            }
        }

        private void _listBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (e.AddedItems.Count != 1) {
                _txtInfo.Text = "Please select only one item at a time!!";
                return;
            }

            string strPath = e.AddedItems[0] as string;
            _txtInfo.Text = System.IO.Path.GetFileName(strPath);
            _txtInfo.Text += GDALInfo.getGdalInfo(strPath);
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
            Properties.Settings.Default.SavedWindowWidth = this.Width;
            Properties.Settings.Default.SavedWindowHeight = this.Height;
            Properties.Settings.Default.SavedGridSplitterPos = _rowDef0.ActualHeight;
            Properties.Settings.Default.Save();
        }

        private void Close_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
            e.CanExecute = true;
        }

        private void Close_Executed(object sender, ExecutedRoutedEventArgs e) {
            this.Close();
        }

        private void Help_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
            e.CanExecute = true;
        }

        private void Help_Executed(object sender, ExecutedRoutedEventArgs e) {
            string strAssemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            //string strPublishVersion = System.Deployment.Application.ApplicationDeployment.
            //    CurrentDeployment.CurrentVersion.ToString();
            string strAbout = "Assembly Version: " + strAssemblyVersion;
            MessageBox.Show(strAbout, "NSTDA GisTool");
        }
    }
}
